﻿using System.Data;
using System.Data.SqlClient;

namespace Adams.Public.DataAccess
{
    public static class PressReleases
    {
        public static Adams.Public.DataTransfer.PressReleases.GetPressReleases GetPressReleases()
        {
            var data = new Adams.Public.DataTransfer.PressReleases.GetPressReleases();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT COUNT(*) AS TotalRecords 
FROM PressReleases.PressReleases P

SELECT P.*
FROM PressReleases.PressReleases P
ORDER BY P.ReleaseDate DESC
", CommandType.Text))
            {
                dr.Read();
                data.Count = dr["TotalRecords"].DbInt();

                dr.NextResult();
                data.PressReleases = new Adams.Shared.Entities.PressReleases.PressReleaseCollection();
                while (dr.Read())
                {
                    var pressrelease = new Adams.Shared.Entities.PressReleases.PressRelease();
                    pressrelease.ID = dr["PressReleaseID"].DbInt();
                    pressrelease.Title = dr["Title"].DbString();
                    pressrelease.ShortDescription = dr["ShortDescription"].DbString();
                    pressrelease.ReleaseDate = dr["ReleaseDate"].DbDate();
                    data.PressReleases.Add(pressrelease);
                }
            }

            return data;
        }

        public static Adams.Public.DataTransfer.PressReleases.GetPressRelease GetPressRelease(int id)
        {
            var data = new Adams.Public.DataTransfer.PressReleases.GetPressRelease();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT A.*
FROM PressReleases.PressReleases A
WHERE A.PressReleaseID = @PressReleaseID
", CommandType.Text,
    DB.CreateParameter("@PressReleaseID", id)))
            {
                var PressRelease = new Shared.Entities.PressReleases.PressRelease();

                if (dr.HasRows)
                {
                    dr.Read();
                    PressRelease.ID = dr["PressReleaseID"].DbInt();
                    PressRelease.Title = dr["Title"].DbString();
                    PressRelease.ReleaseDate = dr["ReleaseDate"].DbDate();
                    PressRelease.ShortDescription = dr["ShortDescription"].DbString();
                    PressRelease.LongDescription = dr["LongDescription"].DbString();
                }

                data.PressRelease = PressRelease;

                return data;
            }
        }
    }
}
